package com.xuecheng.teaching.controller;

import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.dto.WorkChaDto;
import com.xuecheng.commons.model.dto.WorkDto;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.WorkVo;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.teaching.domain.Work;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.teaching.service.WorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 作业前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class WorkController {

    @Autowired
    private WorkService workService;

    //作业分页查询
    @PostMapping("/work/list")
    public ResponseResult findByPage(PageRequest params , @RequestBody(required = false) WorkChaDto dto) {
        return workService.findByPage(params,dto);
    }

    //作业添加和修改
    @PostMapping("/work")
    public ResponseResult savework(@RequestBody WorkDto dto) {
        return workService.savework(dto);
    }

    //作业删除
    @DeleteMapping("/work/{workId}")
       public ResponseResult deletework(@PathVariable("workId") Long workId) {
        return workService.deletework(workId);
    }
    //根基id查询
    @GetMapping("/work/{workId}")
    public ResponseResult findById(@PathVariable("workId") Long workId) {
        // 调用service查询
        System.err.println(AuthInfoHolder.getAuthInfo());
        Work work = workService.getById(workId);
        // 封装vo
        WorkVo workVo = BeanHelper.copyProperties(work, WorkVo.class);
        workVo.setWorkId(work.getId());
        // 返回vo
        return ResponseResult.okResult(workVo);
    }
}